﻿@model IEnumerable<Bordspil.Models.Highscore>

@{
    ViewBag.Title = "Index";
}

<ul id="group-menu">
    <li><a href="#1" id="1" class="high-game">Kindo</a></li> 
    <li><a href="#2" id="2" class="high-game">TicTacToe</a></li>
    <li><a href="#groups" id="hopar" class="high-game">Hopar</a></li>
</ul>

<div id="groups">
    <div class ="group-name"><h3></h3></div>
    <ul id="group-points"></ul><ul id="the-points"></ul>
<table id="user-points" class="highscore-table">

        <tbody>

        </tbody>
          

 
</table>


</div>


<script>
    $(document).ready(function () {
        //tegar tessi sida loadast er sott highscore notanda ur leik med id 1
        GetUserPointsByGameId("1")
        function GetUserPointsByGameId(gameId) {
            //ser um ad prenta stigatoflu notenda i gefnum leik
            $.ajax({
                type: "GET",
                corntentType: "application/json; charset=utf-8",
                url: "/Highscore/GetHighscoreFromGameId",
                data: { "gameId": gameId },
                dataType: "json",
                success: function (data) {
                    $(".group-name h3").append(data[0].Games.GameName);
                    var addOne = parseInt(1);
                    for (var i = 0; i < data.length; i++) {
                        var nr = i;
                        nr = parseInt(nr) + addOne;
                        //console.log("Notandi " + data[i].UserOrGroupName + " er med " + data[i].UserOrGroupPoints);

                        $("#user-points").append("<tr><td>" + nr + "</td><td><a href=/Account/UserProfile?userName=" + data[i].UserOrGroupName + "> " + data[i].UserOrGroupName + "</td><td>" + data[i].UserOrGroupPoints + "</td></tr>");
                    }

                }
            })
        }

        $(".high-game").click(function () {
            $("#the-points").empty();
            $(".group-name h3").empty();
            $("#group-points").empty();
            $("tbody").empty();
            //console.log($(this).attr('id'));
            var goto = $(this).attr('id');
            if (goto === "hopar") {
                //ef valid er ad skoda stig hopa er farid i tad fall sem birtir heildar stig allra hopa
                GetAllGroups();
            } else {

                GetUserPointsByGameId(goto);
            }
        });

        

        function GetAllGroups() {
            //byrja a ad saekja allar gruppur
            $(".group-name h3").append("Heildar stig allra hopa");
            $.ajax({
                type: "GET",
                corntentType: "application/json; charset=utf-8",
                url: "/Group/GetAllGroups",
                data: "{ }",
                dataType: "json",
                success: function (data) {
                    for (var i = 0; i < data.length; i++) {
                        var groupName = encodeURI(data[i].GroupName);
                        var sendData = {
                            "groupName": data[i].GroupName
                        };
                        $("#group-points").append("<li><a href=/Group/GroupProfile?groupName=" + groupName + "> " + data[i].GroupName + "</li>");

                        //console.log(groupName);

                         $.ajax({
                            type: "GET",
                            corntentType: "application/json; charset=utf-8",
                            url: "/Highscore/GetGroupPoints",
                            data: sendData,
                            dataType: "json",
                            success: function (stig) {
                               //saekji svo stig gruppunar og prenta ut stigin
                                $("#the-points").append("<li>" + stig + "</li>");
                                
                            }

                           });
                       
                    } //for lykkja lokast

                }
            });
        }

       
    });


</script>







